#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1+

DEVPKGS=(
    pkg-config
    liblzma-dev
    libcurl4-openssl-dev
    libssl-dev
    libacl1-dev
    libfuse-dev
    zlib1g-dev
    libzstd-dev
    libudev-dev
)

echo
echo "============= Installing amd64 build dependencies =============="
set -eu
(
    set -x

    sudo rm /etc/apt/sources.list.d/*

    sudo apt install -y software-properties-common
    sudo add-apt-repository -y ppa:jonathonf/python-3.6
    sudo add-apt-repository -y ppa:ginggs/backports
    sudo apt -y update
    sudo apt -y --no-install-recommends install \
         python3.6 \
         squashfs-tools

    sudo dpkg --add-architecture i386
    sudo apt-get update

    sudo apt -y --no-install-recommends install \
         rsync \
         python3-pip \
         python3-setuptools \
         python3-wheel \
         python-sphinx \
         "${DEVPKGS[@]}"
    python3.6 -m pip install --user meson ninja
)

echo
echo "============= Building amd64 =============="
(
    set -x
    meson build
    ninja -C build
)

echo
echo "============= Running amd64 tests as user =============="
(
    set -x
    ninja -C build test
)

echo
echo "============= Running amd64 tests as root =============="
(
    set -x
    sudo CASYNC_TEST_NBD=0 $(which ninja) -C build test
)

echo
echo "============= Installing i386 build dependencies =============="

# library -dev packages are not co-installable for multiple architectures,
# so this can't go into the setup step
(
    set -x
    # both arch versions provide /usr/bin/curl-config, which can't go well
    sudo apt remove -y libcurl4-openssl-dev
    sudo apt-get install -y --no-install-recommends \
         gcc-multilib \
         libgcc-5-dev:i386 \
         "${DEVPKGS[@]/%/:i386}"
)

echo
echo "============= Building i386 =============="
(
    set -x
    export PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu
    CC=gcc-5 CFLAGS=-m32 LDFLAGS=-m32 meson build-i386
    ninja -C build-i386
)

echo
echo "============= Running i386 tests as user =============="
(
    set -x
    linux32 ninja -C build-i386 test
)

echo
echo "============= Running i386 tests as root =============="
(
    set -x
    sudo CASYNC_TEST_NBD=0 linux32 $(which ninja) -C build-i386 test
)
